package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constant.RabbitConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 封捷
 * @create-date: 2022/9/12 15:23
 */
@Configuration
public class OrderCancelMQConfig {

    @Bean
    public CustomExchange getCustomExchange() {

        // 1、指定交换机的类型
        String type = "x-delay-message";

        // 2、封装创建交换机对象的参数
        Map<String, Object> argumentsMap = new HashMap<>();
        argumentsMap.put("x-delay-type", "direct");

        // 3、创建对象并返回
        return new CustomExchange(
                RabbitConst.EXCHANGE_DIRECT_ORDER_CANCEL,
                type,
                true,
                false,
                argumentsMap);
    }

    @Bean
    public Queue getDelayQueue() {
        return new Queue(RabbitConst.QUEUE_ORDER_CANCEL, true, false, false);
    }

    @Bean
    public Binding getBinding() {
        return BindingBuilder
                .bind(getDelayQueue())
                .to(getCustomExchange())
                .with(RabbitConst.ROUTING_ORDER_CANCEL)
                .noargs();
    }

}
